//
//  NSData+QSCategory.h
//  QSCategoriesOC_Example
//
//  Created by SQ on 2019/12/18.
//  Copyright © 2019 QiaoShi. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface NSData (QSCategory)

#pragma mark - Hash
- (NSString *)qs_md2String;
- (NSData *)qs_md2Data;
- (NSString *)qs_md4String;
- (NSData *)qs_md4Data;
- (NSString *)qs_md5String;
- (NSData *)qs_md5Data;

- (NSString *)qs_sha1String;
- (NSData *)qs_sha1Data;
- (NSString *)qs_sha224String;
- (NSData *)qs_sha224Data;
- (NSString *)qs_sha256String;
- (NSData *)qs_sha256Data;
- (NSString *)qs_sha384String;
- (NSData *)qs_sha384Data;
- (NSString *)qs_sha512String;
- (NSData *)qs_sha512Data;

- (NSString *)qs_hmacMD5StringWithKey:(NSString *)key;
- (NSData *)qs_hmacMD5DataWithKey:(NSData *)key;
- (NSString *)qs_hmacSHA1StringWithKey:(NSString *)key;
- (NSData *)qs_hmacSHA1DataWithKey:(NSData *)key;
- (NSString *)qs_hmacSHA224StringWithKey:(NSString *)key;
- (NSData *)qs_hmacSHA224DataWithKey:(NSData *)key;
- (NSString *)qs_hmacSHA256StringWithKey:(NSString *)key;
- (NSData *)qs_hmacSHA256DataWithKey:(NSData *)key;
- (NSString *)qs_hmacSHA384StringWithKey:(NSString *)key;
- (NSData *)qs_hmacSHA384DataWithKey:(NSData *)key;
- (NSString *)qs_hmacSHA512StringWithKey:(NSString *)key;
- (NSData *)qs_hmacSHA512DataWithKey:(NSData *)key;

- (NSString *)qs_crc32String;
/**
 Returns crc32 hash.
 */
- (uint32_t)qs_crc32;


#pragma mark - Encrypt and Decrypt
/**
 Returns an encrypted NSData using AES.
 
 @param key   A key length of 16, 24 or 32 (128, 192 or 256bits).
 
 @param iv    An initialization vector length of 16(128bits).
              Pass nil when you don't want to use iv.
 
 @return      An NSData encrypted, or nil if an error occurs.
 */
- (nullable NSData *)qs_aes256EncryptWithKey:(NSData *)key iv:(nullable NSData *)iv;

/**
 Returns an decrypted NSData using AES.
 
 @param key   A key length of 16, 24 or 32 (128, 192 or 256bits).
 
 @param iv    An initialization vector length of 16(128bits).
              Pass nil when you don't want to use iv.
 
 @return      An NSData decrypted, or nil if an error occurs.
 */
- (nullable NSData *)qs_aes256DecryptWithkey:(NSData *)key iv:(nullable NSData *)iv;


#pragma mark - Encode and decode

/**
 Returns string decoded in UTF8.
 */
- (nullable NSString *)qs_utf8String;

/**
 Returns a uppercase NSString in HEX.
 */
- (nullable NSString *)qs_hexString;

/**
 Returns an NSData from hex string.
 
 @param hexString   The hex string which is case insensitive.
 
 @return a new NSData, or nil if an error occurs.
 */
+ (nullable NSData *)qs_dataWithHexString:(NSString *)hexString;

/**
 Returns an NSString for base64 encoded.
 */
- (nullable NSString *)qs_base64EncodedString;

/**
 Returns an NSData from base64 encoded string.
 
 @warning This method has been implemented in iOS7.
 
 @param base64EncodedString  The encoded string.
 */
+ (nullable NSData *)qs_dataWithBase64EncodedString:(NSString *)base64EncodedString;

/**
 Returns an NSDictionary or NSArray for decoded self.
 Returns nil if an error occurs.
 */
- (nullable id)qs_jsonValueDecoded;


#pragma mark - Inflate and deflate
/**
 Decompress data from gzip data.
 @return Inflated data.
 */
- (nullable NSData *)qs_gzipInflate;

/**
 Comperss data to gzip in default compresssion level.
 @return Deflated data.
 */
- (nullable NSData *)qs_gzipDeflate;

/**
 Decompress data from zlib-compressed data.
 @return Inflated data.
 */
- (nullable NSData *)qs_zlibInflate;

/**
 Comperss data to zlib-compressed in default compresssion level.
 @return Deflated data.
 */
- (nullable NSData *)qs_zlibDeflate;


#pragma mark - Others
/**
 Create data from the file in main bundle (similar to [UIImage imageNamed:]).
 
 @param name The file name (in main bundle).
 
 @return A new data create from the file.
 */
+ (nullable NSData *)qs_dataPathNamed:(NSString *)name;


@end

NS_ASSUME_NONNULL_END
